import './index.js';  // 导入所有页面共用的功能 
import { ShowSearch, show_content } from './index.js';

// 图片加载错误处理
function handleImageError() {
    document.querySelectorAll('img').forEach(img => {
        if (!img.hasAttribute('onerror')) {
            img.onerror = function() {
                if (!this.src.includes('placeholder.png')) {
                    this.src = './images/placeholder.png';
                    this.classList.add('img-error');
                }
            };
        }
    });
}

// 图片预加载函数
function preloadImages(imageUrls) {
    imageUrls.forEach(url => {
        const img = new Image();
        img.onload = function() {
            console.log('Preloaded:', url);
        };
        img.onerror = function() {
            console.error('Failed to preload:', url);
        };
        img.src = url;
    });
}

// 初始化函数
function init() {
    handleImageError();
    
    // 监听 DOM 变化，处理动态加载的图片
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            if (mutation.addedNodes.length) {
                handleImageError();
            }
        });
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
}

// 页面加载完成后初始化
if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
} else {
    init();
}

// 将函数暴露到全局作用域
window.ShowSearch = ShowSearch;
window.show_content = show_content;
window.preloadImages = preloadImages; 